package com.chenyuxin.ilp.controller.file;

import com.chenyuxin.ilp.exception.BaseException;
import com.chenyuxin.ilp.model.dto.file.DeleteShareUserDto;
import com.chenyuxin.ilp.model.dto.file.AddShareUserDto;
import com.chenyuxin.ilp.model.dto.file.ChangeShareDto;
import com.chenyuxin.ilp.model.dto.file.GetShareUserDto;
import com.chenyuxin.ilp.model.enums.exception.ExceptionEnums;
import com.chenyuxin.ilp.model.pojo.Result;
import com.chenyuxin.ilp.service.file.SharePermissionService;
import com.chenyuxin.ilp.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/file/share")
public class SharePermissionController {
    @Autowired
    private SharePermissionService sharePermissionService;
    // 设置文件夹为共享文件夹
    @PutMapping
    public Result setFolderOrFileShare(@RequestBody ChangeShareDto changeShareDto){
        String userId = SecurityUtil.getUserId();
        Long id = changeShareDto.getId();
        Integer isShare = changeShareDto.getIsShare();
        Integer type = changeShareDto.getType();
        if(type==1&&isShare==0){
            return sharePermissionService.setFileNoshare(id,userId);
        } else if (type==1&&isShare==1) {
            return sharePermissionService.setFileShare(id,userId);
        } else if (type==2&&isShare==0) {
            return sharePermissionService.setFolderNoshare(id,userId);
        } else if (type==2&&isShare==1) {
            return sharePermissionService.setFolderShare(id,userId);
        }
        throw new BaseException(ExceptionEnums.SYSTEM_ERROR);
    }
    @PostMapping("/adduser")
    public Result addShareFileOrFolderUser(@RequestBody AddShareUserDto addShareUserDto){
        String sharedUserId = addShareUserDto.getSharedUserId();
        Integer type = addShareUserDto.getType();
        Long id = addShareUserDto.getId();
        String userId = SecurityUtil.getUserId();
        if(type==1){
            return sharePermissionService.addFilePermission(id,sharedUserId,userId);
        } else if (type==2) {
            return sharePermissionService.addFolderPermission(id,sharedUserId,userId);
        }
        throw new BaseException(ExceptionEnums.SYSTEM_ERROR);
    }
    // 查询用户在某文件所属者的共享文件树型结构
    @GetMapping("/tree")
    public Result selectHostFileTree(@RequestParam("hostuserid")String hostUserId){
        String userId = SecurityUtil.getUserId();
        return sharePermissionService.selectHostFileTree(hostUserId,userId);
    }
    @GetMapping("/preview/{fileid}")
    public Result previewShareFile(@PathVariable("fileid")Long fileId){
        String userId = SecurityUtil.getUserId();
        return sharePermissionService.previewShareFile(fileId,userId);
    }
    @GetMapping("/download/{fileid}")
    public HttpEntity<byte[]> downloadFile(@PathVariable("fileid")Long fileId){
        String userId = SecurityUtil.getUserId();
        return sharePermissionService.downloadFile(fileId,userId);
    }
    @GetMapping("/user")
    public Result getShareUser(@RequestParam Integer type,@RequestParam Long id){
        String userId = SecurityUtil.getUserId();
        if(type==1){
            return sharePermissionService.getShareFileUser(id,userId);
        } else if (type==2) {
            return sharePermissionService.getShareFolderUser(id,userId);
        }
        throw new BaseException(ExceptionEnums.SYSTEM_ERROR);
    }
    // 用户查询自己能访问哪些共享文件
    @GetMapping
    public Result getShare(){
        String userId = SecurityUtil.getUserId();
        return sharePermissionService.getShare(userId);
    }
    @DeleteMapping("/user")
    public Result deleteShareUser(@RequestBody DeleteShareUserDto deleteShareUserDto){
        String userId = SecurityUtil.getUserId();
        return sharePermissionService.deleteShareUser(deleteShareUserDto,userId);
    }
    // 查询共享文件夹下的字内容
    @GetMapping("/{parentid}")
    public Result getFolderSon(@PathVariable("parentid") Long parentId){
        String userId = SecurityUtil.getUserId();
        return sharePermissionService.getFolderSon(parentId,userId);
    }
}
